/*
 * MainForm.java
 *
 * Created on __DATE__, __TIME__
 */

package com.calcgpa.desktop;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;
import java.util.regex.Pattern;

import javax.swing.JOptionPane;

import com.calcgpa.core.CalcGPA;
import com.calcgpa.core.CalcGPAForSwing;
import com.calcgpa.core.vo.Score;
import com.calcgpa.core.vo.User;
import com.calcgpa.util.CalcGPAMethod;
import com.calcgpa.util.Property;

/**
 *
 * @author  Sora
 */
public class MainForm extends javax.swing.JFrame {

	/**
	 * 
	 */
	private static final long serialVersionUID = -4383149799572598796L;

	/** Creates new form MainForm */
	public MainForm() {
		initComponents();
	}

	//GEN-BEGIN:initComponents
	// <editor-fold defaultstate="collapsed" desc="Generated Code">
	private void initComponents() {

		jScrollPane1 = new javax.swing.JScrollPane();
		jTextArea1 = new javax.swing.JTextArea();
		jLabel1 = new javax.swing.JLabel();
		jLabel2 = new javax.swing.JLabel();
		jTextField1 = new javax.swing.JTextField();
		jPasswordField1 = new javax.swing.JPasswordField();
		jLabel3 = new javax.swing.JLabel();
		jComboBox1 = new javax.swing.JComboBox();
		jCheckBox1 = new javax.swing.JCheckBox();
		jButton1 = new javax.swing.JButton();
		jButton2 = new javax.swing.JButton();
		jLabel4 = new javax.swing.JLabel();
		jCheckBox2 = new javax.swing.JCheckBox();
		jCheckBox3 = new javax.swing.JCheckBox();
		jLabel5 = new javax.swing.JLabel();

		jTextArea1.setColumns(20);
		jTextArea1.setRows(5);
		jScrollPane1.setViewportView(jTextArea1);

		setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
		setTitle("\u5de5\u5927\u539f\u521b\u7ee9\u70b9\u8ba1\u7b97\u5668 v1.1");
		setResizable(false);
		addWindowListener(new java.awt.event.WindowAdapter() {
			public void windowOpened(java.awt.event.WindowEvent evt) {
				formWindowOpened(evt);
			}
		});

		jLabel1.setText("\u7528\u6237\u540d");

		jLabel2.setText("\u5bc6\u7801");

		jLabel3.setText("\u5b66\u671f");

		jComboBox1.setModel(new javax.swing.DefaultComboBoxModel(new String[] {
				"Item 1", "Item 2", "Item 3", "Item 4" }));
		List<String> termList = new ArrayList<String>();
		Calendar cal = Calendar.getInstance();
		int year = cal.get(Calendar.YEAR);
		for (int i = 2007; i <= year - 1; i++) {
			termList.add(i + "/" + (i + 1) + "(" + 1 + ")");
			termList.add(i + "/" + (i + 1) + "(" + 2 + ")");
		}
		termList.add("----------------------------------------");
		for (int i = 2007; i <= year - 1; i++) {
			termList.add(i + "/" + (i + 1));
		}
		jComboBox1.setModel(new javax.swing.DefaultComboBoxModel(termList
				.toArray()));
		jComboBox1.setSelectedIndex(termList.size() - year + 2005);

		jCheckBox1.setText("\u4f53\u80b2\u662f\u5426\u8ba1\u7b97\u5728\u5185");

		jButton1.setText("\u8ba1\u7b97");
		jButton1.addActionListener(new java.awt.event.ActionListener() {
			public void actionPerformed(java.awt.event.ActionEvent evt) {
				jButton1ActionPerformed(evt);
			}
		});

		jButton2.setText("\u9000\u51fa");
		jButton2.addActionListener(new java.awt.event.ActionListener() {
			public void actionPerformed(java.awt.event.ActionEvent evt) {
				jButton2ActionPerformed(evt);
			}
		});

		jLabel4.setFont(new java.awt.Font("微软雅黑", 0, 36));
		jLabel4.setText("\u5de5\u5927\u539f\u521b\u7ee9\u70b9\u8ba1\u7b97\u5668");

		jCheckBox2.setText("\u8bb0\u4f4f\u7528\u6237\u540d");

		jCheckBox3.setText("\u8bb0\u4f4f\u5bc6\u7801");
		jCheckBox3.setActionCommand("jCheckBox3");

		jLabel5.setText("<html>\u4f5c\u8005\u90ae\u7bb1\uff1awksora@vip.qq.com<br/>\u6b22\u8fce\u63d0\u4ea4bug\u6216\u8005\u4e0e\u6211\u4ea4\u6d41\u3002</html>");

		javax.swing.GroupLayout layout = new javax.swing.GroupLayout(
				getContentPane());
		getContentPane().setLayout(layout);
		layout.setHorizontalGroup(layout
				.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
				.addGroup(
						layout.createSequentialGroup()
								.addGroup(
										layout.createParallelGroup(
												javax.swing.GroupLayout.Alignment.LEADING,
												false)
												.addGroup(
														layout.createSequentialGroup()
																.addGap(122,
																		122,
																		122)
																.addGroup(
																		layout.createParallelGroup(
																				javax.swing.GroupLayout.Alignment.TRAILING)
																				.addComponent(
																						jButton1,
																						javax.swing.GroupLayout.PREFERRED_SIZE,
																						72,
																						javax.swing.GroupLayout.PREFERRED_SIZE)
																				.addComponent(
																						jCheckBox2))
																.addPreferredGap(
																		javax.swing.LayoutStyle.ComponentPlacement.RELATED))
												.addGroup(
														javax.swing.GroupLayout.Alignment.TRAILING,
														layout.createSequentialGroup()
																.addContainerGap(
																		javax.swing.GroupLayout.DEFAULT_SIZE,
																		Short.MAX_VALUE)
																.addGroup(
																		layout.createParallelGroup(
																				javax.swing.GroupLayout.Alignment.LEADING)
																				.addComponent(
																						jLabel2)
																				.addComponent(
																						jLabel1)
																				.addComponent(
																						jLabel3,
																						javax.swing.GroupLayout.PREFERRED_SIZE,
																						24,
																						javax.swing.GroupLayout.PREFERRED_SIZE))
																.addGap(29, 29,
																		29)))
								.addGroup(
										layout.createParallelGroup(
												javax.swing.GroupLayout.Alignment.LEADING)
												.addGroup(
														layout.createParallelGroup(
																javax.swing.GroupLayout.Alignment.LEADING)
																.addComponent(
																		jComboBox1,
																		0,
																		212,
																		Short.MAX_VALUE)
																.addComponent(
																		jPasswordField1,
																		javax.swing.GroupLayout.DEFAULT_SIZE,
																		212,
																		Short.MAX_VALUE)
																.addComponent(
																		jTextField1,
																		javax.swing.GroupLayout.DEFAULT_SIZE,
																		212,
																		Short.MAX_VALUE))
												.addGroup(
														layout.createSequentialGroup()
																.addGap(10, 10,
																		10)
																.addComponent(
																		jCheckBox3)
																.addPreferredGap(
																		javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
																.addGroup(
																		layout.createParallelGroup(
																				javax.swing.GroupLayout.Alignment.LEADING)
																				.addComponent(
																						jButton2,
																						javax.swing.GroupLayout.PREFERRED_SIZE,
																						79,
																						javax.swing.GroupLayout.PREFERRED_SIZE)
																				.addComponent(
																						jCheckBox1))))
								.addGap(131, 131, 131))
				.addGroup(
						javax.swing.GroupLayout.Alignment.TRAILING,
						layout.createSequentialGroup()
								.addContainerGap(112, Short.MAX_VALUE)
								.addComponent(jLabel4,
										javax.swing.GroupLayout.PREFERRED_SIZE,
										409,
										javax.swing.GroupLayout.PREFERRED_SIZE)
								.addGap(36, 36, 36))
				.addGroup(
						javax.swing.GroupLayout.Alignment.TRAILING,
						layout.createSequentialGroup()
								.addContainerGap(353, Short.MAX_VALUE)
								.addComponent(jLabel5,
										javax.swing.GroupLayout.PREFERRED_SIZE,
										192,
										javax.swing.GroupLayout.PREFERRED_SIZE)
								.addContainerGap()));
		layout.setVerticalGroup(layout
				.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
				.addGroup(
						layout.createSequentialGroup()
								.addContainerGap()
								.addComponent(jLabel4,
										javax.swing.GroupLayout.PREFERRED_SIZE,
										58,
										javax.swing.GroupLayout.PREFERRED_SIZE)
								.addGap(18, 18, 18)
								.addGroup(
										layout.createParallelGroup(
												javax.swing.GroupLayout.Alignment.BASELINE)
												.addComponent(
														jTextField1,
														javax.swing.GroupLayout.PREFERRED_SIZE,
														javax.swing.GroupLayout.DEFAULT_SIZE,
														javax.swing.GroupLayout.PREFERRED_SIZE)
												.addComponent(jLabel1))
								.addPreferredGap(
										javax.swing.LayoutStyle.ComponentPlacement.RELATED)
								.addGroup(
										layout.createParallelGroup(
												javax.swing.GroupLayout.Alignment.BASELINE)
												.addComponent(
														jPasswordField1,
														javax.swing.GroupLayout.PREFERRED_SIZE,
														javax.swing.GroupLayout.DEFAULT_SIZE,
														javax.swing.GroupLayout.PREFERRED_SIZE)
												.addComponent(jLabel2))
								.addPreferredGap(
										javax.swing.LayoutStyle.ComponentPlacement.RELATED)
								.addGroup(
										layout.createParallelGroup(
												javax.swing.GroupLayout.Alignment.BASELINE)
												.addComponent(
														jComboBox1,
														javax.swing.GroupLayout.PREFERRED_SIZE,
														javax.swing.GroupLayout.DEFAULT_SIZE,
														javax.swing.GroupLayout.PREFERRED_SIZE)
												.addComponent(jLabel3))
								.addPreferredGap(
										javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
								.addGroup(
										layout.createParallelGroup(
												javax.swing.GroupLayout.Alignment.BASELINE)
												.addComponent(jCheckBox1)
												.addComponent(jCheckBox2)
												.addComponent(jCheckBox3))
								.addPreferredGap(
										javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
								.addGroup(
										layout.createParallelGroup(
												javax.swing.GroupLayout.Alignment.BASELINE)
												.addComponent(jButton1)
												.addComponent(jButton2))
								.addPreferredGap(
										javax.swing.LayoutStyle.ComponentPlacement.RELATED)
								.addComponent(jLabel5,
										javax.swing.GroupLayout.PREFERRED_SIZE,
										52,
										javax.swing.GroupLayout.PREFERRED_SIZE)));

		pack();
	}// </editor-fold>
	//GEN-END:initComponents

	private void formWindowOpened(java.awt.event.WindowEvent evt) {
		Property prop = new Property();
		prop.load();
		if (prop.getProperty("com.calcgpa.isremuser").equals("true")) {
			this.jCheckBox2.setSelected(true);
			String username = prop.getProperty("com.calcgpa.username");
			this.jTextField1.setText(username);
			if (prop.getProperty("com.calcgpa.isrempwd").equals("true")) {
				this.jCheckBox3.setSelected(true);
				String password = prop.getProperty("com.calcgpa.password");
				this.jPasswordField1.setText(password);
			}
		}

	}

	private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
		if (jTextField1.getText() == null || jTextField1.getText().equals("")) {
			JOptionPane.showMessageDialog(null, "请输入用户名");
			return;
		}
		if (Pattern.compile("^[0-9]{12}$").matcher(jTextField1.getText())
				.find() == false) {
			JOptionPane.showMessageDialog(null, "用户名格式不正确");
			return;
		}
		if (jPasswordField1.getPassword() == null
				|| jPasswordField1.getPassword().length == 0) {
			JOptionPane.showMessageDialog(null, "请输入密码");
			return;
		}
		if (jComboBox1.getSelectedItem().toString().indexOf("------") != -1) {
			JOptionPane.showMessageDialog(null, "请选择正确的学期");
			return;
		}
		String username = jTextField1.getText();
		String password = String.valueOf(jPasswordField1.getPassword());
		String term = jComboBox1.getItemAt(jComboBox1.getSelectedIndex())
				.toString();
		boolean isPE = jCheckBox1.isSelected();
		CalcGPAForSwing calcgpa = new CalcGPAForSwing();

		List<Score> scores = new ArrayList<Score>();
		User user = new User();
		int state = calcgpa.doCalc(username, password, term, scores, user);

		if (state == CalcGPA.LOGIN_ERROR) {
			JOptionPane.showMessageDialog(null, "登录失败，请检查用户名和密码，或者检查网络链接是否正常。");
			return;
		}

		Object[][] o = new Object[scores.size()][6];
		for (int i = 0; i < scores.size(); i++) {
			o[i][0] = scores.get(i).getTerm();
			o[i][1] = scores.get(i).getCourse();
			o[i][2] = scores.get(i).getScore();
			o[i][3] = scores.get(i).getType();
			o[i][4] = scores.get(i).getTime();
			o[i][5] = scores.get(i).getCredit();
		}

		ShowGPA dialog = new ShowGPA(this, true);
		dialog.jTable1.setModel(new javax.swing.table.DefaultTableModel(o,
				new String[] { "实际学期", "执行课程名称", "考试性质", "成绩", "学时", "学分" }));

		CalcGPAMethod calc = new CalcGPAMethod();
		dialog.jLabel1.setText("平均绩点为：" + calc.doCalc(scores, isPE));
		dialog.jLabel2.setText(user.getId() + "      " + user.getName()
				+ "      " + user.getClasses());
		dialog.setLocationRelativeTo(this);
		dialog.setVisible(true);
		updateProperty();

	}

	public void updateProperty() {
		Property prop = new Property();
		if (this.jCheckBox2.getSelectedObjects()[0] != null) {
			prop.setProperty("com.calcgpa.isremuser", "true");
			prop.setProperty("com.calcgpa.username", this.jTextField1.getText());
			if (this.jCheckBox3.getSelectedObjects()[0] != null) {
				prop.setProperty("com.calcgpa.isrempwd", "true");
				prop.setProperty("com.calcgpa.password",
						String.valueOf(this.jPasswordField1.getPassword()));
			} else {
				prop.setProperty("com.calcgpa.isrempwd", "false");
				prop.setProperty("com.calcgpa.password", "");
			}
		} else {
			prop.setProperty("com.calcgpa.isremuser", "false");
			prop.setProperty("com.calcgpa.isrempwd", "false");
			prop.setProperty("com.calcgpa.username", "");
			prop.setProperty("com.calcgpa.password", "");
		}
		if (this.jCheckBox2.getSelectedObjects()[0] != null) {
			prop.setProperty("com.calcgpa.ispe", "true");
		} else {
			prop.setProperty("com.calcgpa.ispe", "false");
		}
		prop.save();
	}

	private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {
		System.exit(0);

	}

	/**
	 * @param args the command line arguments
	 */
	public static void main(String args[]) {
		try {
			javax.swing.UIManager
					.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
		} catch (Exception e) {
			e.printStackTrace();
		}
		/*
		java.awt.EventQueue.invokeLater(new Runnable() {
			public void run() {
				new MainForm().setVisible(true);
			}
		});
		 */
		MainForm form = new MainForm();
		form.setLocationRelativeTo(null);
		form.setVisible(true);
	}

	//GEN-BEGIN:variables
	// Variables declaration - do not modify
	private javax.swing.JButton jButton1;
	private javax.swing.JButton jButton2;
	private javax.swing.JCheckBox jCheckBox1;
	private javax.swing.JCheckBox jCheckBox2;
	private javax.swing.JCheckBox jCheckBox3;
	private javax.swing.JComboBox jComboBox1;
	private javax.swing.JLabel jLabel1;
	private javax.swing.JLabel jLabel2;
	private javax.swing.JLabel jLabel3;
	private javax.swing.JLabel jLabel4;
	private javax.swing.JLabel jLabel5;
	private javax.swing.JPasswordField jPasswordField1;
	private javax.swing.JScrollPane jScrollPane1;
	private javax.swing.JTextArea jTextArea1;
	private javax.swing.JTextField jTextField1;
	// End of variables declaration//GEN-END:variables

}